package com.masterclient.app.businessmodule.homemodule.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.model.MeetReportDataBean;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by TMVPHelper on 2017/07/18
 */
public class MeetReportPresenter extends MeetReportContract.Presenter {

    @Override
    public void getNews(String page, String pageSize, String pageName) {
        Observable<CommonBaseResponse<MeetReportDataBean>> observable  = RetrofitUtil2.getInstance().getNews(page, pageSize);;
        if (pageName.equals("news")) {
            observable = RetrofitUtil2.getInstance().getNews(page, pageSize);
        } else if (pageName.equals("historys")) {
            observable = RetrofitUtil2.getInstance().getHistorys(page, pageSize);
        } else if (pageName.equals("views")) {
            observable = RetrofitUtil2.getInstance().getViews(page, pageSize);
        }
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<MeetReportDataBean>>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.getNewsInfoSuccess(bean.data);
            } else {
                mView.getNewsInfoFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.getNewsInfoFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}